package com.baofu.paycore.biz.engine.processor.impl;

import com.baofu.account.service.facade.model.request.UnFreezeReqDTO;
import com.baofu.paycore.biz.engine.command.impl.AccFreezeCommand;
import com.baofu.paycore.biz.engine.processor.BaseProcessor;
import com.baofu.paycore.biz.engine.result.CommandResult;
import com.baofu.paycore.biz.enums.CommandProcessorEnum;
import com.baofu.paycore.biz.external.account.AccountService;
import com.baofu.paycore.biz.external.account.converter.AccountRequestConverter;
import com.baofu.paycore.manager.model.AccountRespBO;
import com.baofu.paycore.service.facade.enums.PayStatusEnums;
import com.system.commons.exception.BizServiceException;
import com.system.commons.sequence.redis.SequenceFacade;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 账务冻结流程
 * <p>
 * 1.
 * </p>
 * User: xin deng  Date: 16/6/24 ProjectName: paycore Version: 1.0
 */
@Slf4j
@Service
public class AccFreezeProcessor implements BaseProcessor<AccFreezeCommand> {

    /**
     * 调用账务充值服务
     */
    @Autowired
    private AccountService accountService;

    /**
     * 业务编号生成服务
     */
    @Autowired
    private SequenceFacade sequenceFacade;

    /**
     * 获取处理器类型
     *
     * @return 命令处理器
     */
    @Override
    public CommandProcessorEnum getProcessorType() {
        return CommandProcessorEnum.ACC_FREEZE_PROCESSOR;
    }

    /**
     * 执行特定支付工具的特定支付类型的批量支付命令，并返回相应的结果
     *
     * @param accFreezeCommand  账户冻结指令
     * @return                  指令执行结果
     */
    @Override
    public Map<String, CommandResult> doProcess(AccFreezeCommand accFreezeCommand) {
        return null;
    }

    /**
     * 执行取消方法
     *
     * @param commands 支付命令
     * @return map          指令执行结果
     */
    @Override
    public Map<String, CommandResult> doCancel(List<AccFreezeCommand> commands) {

        log.info("账务解冻处理器 指令:{}", commands);
        Map<String, CommandResult> result = new HashMap<>();
        CommandResult unfreezeResult = new CommandResult();
        AccFreezeCommand accFreezeCommand = commands.get(0);
        unfreezeResult.setRealRtnFlag(true);
        unfreezeResult.setStatus(PayStatusEnums.FAIL.getCode());
        try {

            UnFreezeReqDTO unFreezeReqDTO = AccountRequestConverter.getUnFreezeReqDTO(accFreezeCommand);
            unFreezeReqDTO.setRequestNo(sequenceFacade.getUniqueSeq());

            AccountRespBO accountRespBO = accountService.unFreeze(accFreezeCommand.getFundDetailNo(),
                    accFreezeCommand.getServiceCode(), unFreezeReqDTO);

            unfreezeResult.setResultVO(accountRespBO);
            unfreezeResult.setStatus(accountRespBO.getStatus());
            unfreezeResult.setRealRtnFlag(accountRespBO.isRealRtn());
            //记录账务系统返回的会计日和凭证号，用于方向交易时使用
            unfreezeResult.setResponseDate(accountRespBO.getAccDate());
            unfreezeResult.setResponseNo(accountRespBO.getReceiptNo());
            unfreezeResult.setCommand(accFreezeCommand);

        } catch (BizServiceException e) {
            log.error("账务解冻指令处理失败:{}", e);
            unfreezeResult.setResultCode(e.getErrorCode().getErrorCode());
            unfreezeResult.setResultMsg(e.getExtraMsg());
        }
        result.put(accFreezeCommand.getCommandNo(), unfreezeResult);

        return result;
    }

}
